using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace QLHXNK.DataAccess
{
    class ItemData
    {
        #region---------------------------------------------Thêm, xoá, sửa-----------------------------------------

        public static int CreateItem(String itemCode, String itemName, String itemSpecs, String itemUnit,String remark)
        {
            try
            {
                string sqlstring = "insert into Item(item_code,item_name,item_specs,item_unit,remark) values('" + itemCode.Replace("'","''") + "','"
                                    + itemName.Replace("'", "''") + "','" + itemSpecs.Replace("'", "''") + "', '" + itemUnit.Replace("'", "''") + "', '" + remark.Replace("'", "''") + "')";
                ConnectionData connectionData = new ConnectionData();
                if (connectionData.Command(sqlstring) != 0)
                    return Int32.Parse(GetItem(itemName).Rows[0]["ID"].ToString());
                return 0;
            }
            catch
            {
                return 0;
            }
        }


        public static int DeleteItem(int itemID)
        {
            try
            {
                string sqlstring = "delete from Item where ID=" + itemID + "";
                ConnectionData connectionData = new ConnectionData();
                return connectionData.Command(sqlstring);
            }
            catch 
            {
                return 0;
            }
        }


        public static int UpdateItem(int itemID, String itemCode, String itemName, String itemSpecs, String itemUnit, String remark)
        {
            try
            {
                string sqlstring = "update Item set item_code='" + itemCode.Replace("'", "''") + "',item_name='" + itemName.Replace("'", "''")
                                    + "',item_specs='" + itemSpecs.Replace("'", "''") + "', item_unit='" + itemUnit.Replace("'", "''")
                                    + "', remark='" + remark.Replace("'", "''") + "' where ID=" + itemID + "";
                ConnectionData connectionData = new ConnectionData();
                return connectionData.Command(sqlstring);
            }
            catch
            {
                return 0;
            }
        }

        #endregion

        #region ------------------------------------------Lấy thông tin item---------------------------------------

        public static DataTable GetItem(String itemName)
        {
            try
            {
                string queryCondition = "";
                if (itemName != "")
                    queryCondition += " item_name = '" + itemName.Replace("'","''")+ "' ";
                string sqlstring = "select * from Item ";
                if (queryCondition != "")
                    sqlstring  += " where " + queryCondition;
                ConnectionData connectionData = new ConnectionData();
                return connectionData.List(sqlstring);
            }
            catch
            {
                return null;
            }
        }
        public static DataTable GetItemInImportNote(int ID)
        {
            try
            {
                string sqlstring = "select * from Import_Note_Detail where item_id=" + ID + " ";
                ConnectionData connectionData = new ConnectionData();
                return connectionData.List(sqlstring);
            }
            catch
            {
                return null;
            }
        }
        #endregion

    }
}
